package org.iteam.core.service.base;

import java.sql.SQLException;
import java.util.List;
import org.iteam.core.Constant;
import org.iteam.core.model.base.BaseArea;
import org.iteam.core.service.BaseParentServiceInterface;
import org.iteam.core.service.BaseService;
import org.iteam.core.service.mysql.impl.base.BaseAreaServiceImpl;
import org.iteam.core.utils.SqlBuilder;

public abstract class BaseAreaService extends BaseService<BaseArea> implements BaseParentServiceInterface<BaseArea> {
	
	private static BaseAreaService serviceMysql = null;
	private static BaseAreaService serviceOracle = null;
	private static String tableName="BASE_AREA";

	public static BaseAreaService getInstance() {
		Constant.DataBaseType dbTypes = Constant.dataBaseType;
		if (dbTypes.equals(Constant.DataBaseType.MYSQL)) {
			return getMySqlInstance();
		} else if (dbTypes.equals(Constant.DataBaseType.ORACLE)) {
			return getOracleInstance();
		}
		return null;
	}

	public static BaseAreaService getMySqlInstance() {
		if (serviceMysql == null)
			serviceMysql = new BaseAreaServiceImpl();
		return serviceMysql;
	}

	public static BaseAreaService getOracleInstance() {
		if (serviceOracle == null)
			serviceOracle = new org.iteam.core.service.oracle.impl.base.BaseAreaServiceImpl();
		return serviceOracle;
	}

	public BaseAreaService() {
		super(BaseArea.class);
	}

	@Override
	public List<BaseArea> query() throws SQLException {
		String SQL = "SELECT AID,PID,NAME,SZCODE,ROME,ZMCODE FROM BASE_AREA ORDER BY AID,NAME ";
		return super.query(SQL);
	}

	public List<BaseArea> query(String parent) throws SQLException {
		String SQL = "SELECT AID,PID,NAME,SZCODE,ROME,ZMCODE FROM BASE_AREA WHERE PID = ? ORDER BY AID,NAME";
		return super.query(SQL, parent);
	}

	@Override
	public long count() throws SQLException {
		String SQL = "SELECT COUNT(1) FROM BASE_AREA";
		return Long.parseLong(super.certain(SQL).toString());
	}

	@Override
	public long count(String parent) throws SQLException {
		if (parent == null || "".equals(parent)) {
			String SQL = "SELECT COUNT(1) FROM BASE_AREA WHERE PID IS NULL";
			return Long.parseLong(super.certain(SQL).toString());
		} else {
			String SQL = "SELECT COUNT(1) FROM BASE_AREA WHERE PID = ?";
			return Long.parseLong(super.certain(SQL, parent).toString());
		}
	}

	public long validateSzcode(String szcode) throws SQLException {
		String SQL = "SELECT COUNT(1) FROM BASE_AREA WHERE SZCODE = ?  ";
		return Long.parseLong(super.certain(SQL, szcode).toString());

	}

	public long validateAid(String aid) throws SQLException {
		String SQL = "SELECT COUNT(1) FROM BASE_AREA WHERE AID = ?  ";
		return Long.parseLong(super.certain(SQL, aid).toString());

	}

	@Override
	public BaseArea show(String id) throws SQLException {
		String SQL = "SELECT AID,PID,NAME,SZCODE,ROME,ZMCODE FROM BASE_AREA WHERE AID=?";
		return super.show(SQL, id);
	}

	@Override
	public boolean create(BaseArea obj) throws SQLException {
		if (obj.getPid() == null || "".equals(obj.getPid()))
			obj.setPid(null);
		SqlBuilder sql=SqlBuilder.createInsert(tableName);
		sql.addField("AID", obj.getAid());
		sql.addField("PID", obj.getPid());
		sql.addField("NAME", obj.getName());
		sql.addField("SZCODE", obj.getSzcode());
		sql.addField("ROME", obj.getRome());
		sql.addField("ZMCODE", obj.getZmcode());
		return super.update(sql.getSql(),sql.getParams());
	}

	@Override
	public boolean update(BaseArea obj) throws SQLException {
		if (obj.getPid() == null || "".equals(obj.getPid()))
			obj.setPid(null);
		SqlBuilder sql=SqlBuilder.createUpdate(tableName);
		sql.addField("PID", obj.getPid());
		sql.addField("NAME", obj.getName());
		sql.addField("SZCODE", obj.getSzcode());
		sql.addField("ROME", obj.getRome());
		sql.addField("ZMCODE", obj.getZmcode());
		sql.addWhere("AID", obj.getAid());
		return super.update(sql.getSql(),sql.getParams());
	}

	@Override
	public boolean delete(String id) throws SQLException {
		return destroy(id);
	}

	@Override
	public boolean destroy(String id) throws SQLException {
		SqlBuilder sql=SqlBuilder.createDelete(tableName);
		sql.addWhere("AID", id);
		return super.update(sql.getSql(),sql.getParams());
	}

	/**
	 * 查询省级、市级地区
	 * @return
	 * @throws SQLException
	 */
	public List<BaseArea> queryProvincialMunicipal() throws SQLException{
		SqlBuilder sql=SqlBuilder.createQuery(tableName, "AID,PID,NAME,SZCODE,ROME,ZMCODE");
		sql.addWhere("PID IS NULL");
		return super.query(sql.getSql(),sql.getParams());
	}
}
